package logic

import (
	"context"
	"fmt"

	"probe-scheduler/internal/svc"
	pb "probe-scheduler/pb/scheduler"

	"github.com/zeromicro/go-zero/core/logx"
)

type PageQueryTaskLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPageQueryTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PageQueryTaskLogic {
	return &PageQueryTaskLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PageQueryTaskLogic) PageQueryTask(in *pb.PageQueryTaskRequest) (*pb.PageQueryTaskResponse, error) {
	// 分页查询
	tasks, total, totalPage, err := l.svcCtx.TaskDao.PageQueryTask(
		l.ctx, in.Page, in.PageSize, in.Status, in.SortBy, in.SortOrder,
	)
	if err != nil {
		return &pb.PageQueryTaskResponse{
			Base: &pb.CommonResponse{Success: false, Message: fmt.Sprintf("分页查询失败: %v", err)},
		}, nil
	}

	return &pb.PageQueryTaskResponse{
		Base:        &pb.CommonResponse{Success: true, Message: "分页查询成功"},
		Tasks:       tasks,
		Total:       total,
		TotalPage:   totalPage,
		CurrentPage: in.Page,
		PageSize:    in.PageSize,
	}, nil
}
